package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: shoppingCartServiceImpl
 * Package: com.sky.service.impl
 * Description:
 *
 * @Author 八云
 * @Create 2025/3/23 19:23
 * @Version 1.0
 */

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper DishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 判断当前商品是否在购物车中
        ShoppingCart shoppingCart = ShoppingCart.builder()
                // 设置用户id
                .userId(BaseContext.getCurrentId())
                // 设置当前时间
                .createTime(LocalDateTime.now())
                // 设置菜品id
                .dishId(shoppingCartDTO.getDishId())
                // 设置套餐id
                .setmealId(shoppingCartDTO.getSetmealId())
                // 设置口味
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .build();

        // 数据库查询结果
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if( list != null && list.size() > 0){
            // 数据库已存在
            list.get(0).setNumber(list.get(0).getNumber() + 1);
            shoppingCartMapper.update(list.get(0));
        }else{
            // 数据库不存在，则添加到购物车，数量默认为1
            shoppingCart.setNumber(1);
            if (shoppingCartDTO.getDishId() != null){
                // 本次添加的是菜品
                // 获取菜品信息
                Dish dish = DishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCartMapper.add(shoppingCart);
            }
            else {
                // 本次添加的是套餐
                // 获取套餐信息
                SetmealDTO byIdWithDish = setmealMapper.getByIdWithDish(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(byIdWithDish.getName());
                shoppingCart.setImage(byIdWithDish.getImage());
                shoppingCart.setAmount(byIdWithDish.getPrice());
                shoppingCartMapper.add(shoppingCart);
            }
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                // 设置用户id
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
         shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        // 判断当前商品是否在购物车中
        ShoppingCart shoppingCart = ShoppingCart.builder()
                // 设置用户id
                .userId(BaseContext.getCurrentId())
                // 设置当前时间
                .createTime(LocalDateTime.now())
                // 设置菜品id
                .dishId(shoppingCartDTO.getDishId())
                // 设置套餐id
                .setmealId(shoppingCartDTO.getSetmealId())
                // 设置口味
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .build();

        // 数据库查询结果
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        list.get(0).setNumber(list.get(0).getNumber() - 1);
        if (list.get(0).getNumber() == 0){
            shoppingCartMapper.delete(list.get(0).getId());
            return;
        }
        shoppingCartMapper.update(list.get(0));
    }
}
